package com.invite.me.ui.registeractivity;

import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.invite.me.R;
import com.invite.me.base.BaseActivity;
import com.invite.me.beans.presenter.RegisterCsmmPresenter;
import com.invite.me.beans.res.YanZhenMaShouBean;
import com.invite.me.util.PhoneFormatCheckUtils;
import com.invite.me.util.ToastUtils;
import com.invite.me.wigets.CountDownTextView;
import com.invite.me.wlqq.Contract;

public class RegisterCsmmActivity extends BaseActivity implements View.OnClickListener,Contract.BaseView{

    private EditText zhuzephone;
    private EditText yanzhengma;
    private EditText shurumima;
    private CountDownTextView yanzhengmasend;
    private ImageView yanjingkanjian;
    private TextView zhuzhephone;
    private boolean mimakanjian=false;
    RegisterCsmmPresenter registerCsmmPresenter;


    @Override
    protected void initView() {
        registerCsmmPresenter=new RegisterCsmmPresenter(this);
        ImageView chahao = findViewById(R.id.chahao);
        zhuzephone = findViewById(R.id.zhuzephone);
        yanzhengma = findViewById(R.id.yanzhengma);
        shurumima = findViewById(R.id.shurumima);
        yanzhengmasend = findViewById(R.id.yanzhengmasend);
        yanjingkanjian = findViewById(R.id.yanjingkanjian);
        zhuzhephone = findViewById(R.id.zhuzhephone);
        yanzhengmasend.setOnClickListener(this);
        yanjingkanjian.setOnClickListener(this);
        zhuzhephone.setOnClickListener(this);
        chahao.setOnClickListener(this);


    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.yanjingkanjian:
                if(mimakanjian){
                    mimakanjian=false;//隐藏
                    shurumima.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }else{
                    mimakanjian=true;//可见
                    shurumima.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }
                break;
            case R.id.yanzhengmasend:
                //验证码
                String phones = zhuzephone.getText().toString();
                if(!TextUtils.isEmpty(phones)){
                    boolean phoneLegal = PhoneFormatCheckUtils.isPhoneLegal(phones);
                    if (phoneLegal) {
                        registerCsmmPresenter.yanzhengma(phones,"3");
                        yanzhengmasend.setCountDownEndText("重新发送")
                                .setCountingTextColor(
                                        ContextCompat.getColor(this, R.color.yanzhengmah)
                                )
                                .setCountEndTextColor(ContextCompat.getColor(this, R.color.yanzhengmah))
                                .setOnCountDownListener(new CountDownTextView.OnCountDownListener() {
                                    @Override
                                    public void onCountDown(int allTime, int remain) {
                                        if (remain <= 0) {
                                            yanzhengmasend.setClickable(true);
                                            yanzhengmasend.setEnabled(true);
                                        }
                                    }
                                })
                                .startCountDown();
                    }else {
                        ToastUtils.show(RegisterCsmmActivity.this, "请输入正确的手机号", Toast.LENGTH_SHORT);

                    }

                }else {
                    ToastUtils.show(RegisterCsmmActivity.this,"请输入手机号");
                }

                break;
            case R.id.zhuzhephone:
                String phonezhuce = zhuzephone.getText().toString();
                String yanzhengmazhuce = yanzhengma.getText().toString();
                String mimazc = shurumima.getText().toString();
                boolean phoneLegal = PhoneFormatCheckUtils.isPhoneLegal(phonezhuce);
                if (phoneLegal) {
                    if(!TextUtils.isEmpty(yanzhengmazhuce)&&yanzhengmazhuce.length()>=4){
                        if(!TextUtils.isEmpty(mimazc)&&mimazc.length()>6){
                            registerCsmmPresenter.zhucephone(phonezhuce,yanzhengmazhuce,mimazc);
                        }else {
                            ToastUtils.show(RegisterCsmmActivity.this, "请输入正确的密码", Toast.LENGTH_SHORT);
                        }

                    }else {
                        ToastUtils.show(RegisterCsmmActivity.this, "请输入正确的验证码", Toast.LENGTH_SHORT);

                    }


                }else {
                    ToastUtils.show(RegisterCsmmActivity.this, "请输入正确的手机号", Toast.LENGTH_SHORT);

                }


                break;
            case R.id.chahao:
                finish();
                break;




        }


    }
    @Override
    protected int getActivityLayoutId() {
        return R.layout.activity_registercsmm;
    }

    @Override
    protected void loadData() {

    }



    @Override
    public void setPresenter(Contract.BasePresenter basePresenter) {
        if (registerCsmmPresenter instanceof Contract.BasePresenter) {
            this.registerCsmmPresenter = (RegisterCsmmPresenter) basePresenter;
        }
    }

    @Override
    public void onDataSucess(Object o) {
        if(o instanceof YanZhenMaShouBean){
            YanZhenMaShouBean yanZhenMaShouBean= (YanZhenMaShouBean) o;
//            String code = yanZhenMaShouBean.getData().getCode();
//            yanzhengma.setText(code);
        }

    }

    @Override
    public void onDataFaile(Object o) {

    }

    @Override
    public void onComplete() {

    }
}